package com.renjun.smweb.service.NoticeService.impl;

import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.commons.enums.EOperators;
import com.renjun.smweb.commons.util.*;
import com.renjun.smweb.dao.bean.InsertParams;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.dao.bean.QueryParams;
import com.renjun.smweb.dao.bean.UpdateParams;
import com.renjun.smweb.dao.pojo.LoginInfo;
import com.renjun.smweb.exceptions.BaseException;
import com.renjun.smweb.service.NoticeService.NoticeService;
import com.renjun.smweb.commons.componet.BaseDaoComponent;
import com.renjun.smweb.dao.bean.InsertParams;
import com.renjun.smweb.dao.bean.Parameter;
import com.renjun.smweb.exceptions.BaseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by RenJun on 2017/2/21.
 */
@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private BaseDaoComponent baseDaoComponent;

    @Override
    public JSONObject queryNotice(Map<String, Object> params) throws BaseException, SQLException, IOException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String userid = (String) params.get("userid");
        if (BaseChecks.hasEmptyStr(userid)) throw new BaseException("用户id不能为空");
        //查询
        String title = (String) params.remove("title");
        QueryParams queryNotice = QueryParams.createQueryParams("vb_queryNotices_lrs");
        //模糊条件
        if (!BaseChecks.hasEmptyStr(title)) {
            queryNotice.addQueryParams(Parameter.createParameter("title", EOperators.类似, StringUtils.removeEmptyCharsOfStr(title)));
        }
        queryNotice.addQueryParamsByMap(CollectionUtils.removeEmptyKeyByParams(params));
        List<Map<String, Object>> mapList = baseDaoComponent.selectDataByParams(queryNotice);
        //类型转换
        StringUtils.clobToStrForList(mapList);
        return ResponseUtils.createSuccessResponseBody("查询成功", mapList);
    }

    @Override
    public JSONObject addNoticeForCity(Map<String, Object> params) throws BaseException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        params.remove("virpath");
        //校验参数
        validateParams(params);
        String mod = (String) params.get("mod");
        if (BaseChecks.hasEmptyStr(mod)) throw new BaseException("公告类型不能为空");
        //添加参数
        params.put("id", UUID.randomUUID().toString());
        params.put("status", "0");
        params.put("lasttime", Format.getDate());
        params.put("userid", loginInfo.getId());
        //添加公告
        InsertParams inserNotice = InsertParams.createInsertParams("t_noticesinfo", params);
        try {
            //日志
            inserNotice.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.insertDataByParams(inserNotice);
        return ResponseUtils.createSuccessResponseBody("添加成功");
    }

    @Override
    public JSONObject modifyNotice(Map<String, Object> params) throws BaseException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        params.remove("virpath");
        //校验参数
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("公告id不能为空");
        //修改
        UpdateParams updateNotice = UpdateParams.createUpdateParams("t_noticesInfo");
        updateNotice.addWhereParameter(Parameter.createParameter("id", id));
        updateNotice.addParamsForMap(params);
        try {
            updateNotice.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateNotice);
        return ResponseUtils.createSuccessResponseBody("修改成功");
    }

    @Override
    public JSONObject cancelNotice(Map<String, Object> params) throws BaseException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("公告id不能为空");
        //删除公告
        UpdateParams updateNotice = UpdateParams.createUpdateParams("t_noticesInfo");
        updateNotice.addWhereParameter(Parameter.createParameter("id", id));
        updateNotice.addParams(Parameter.createParameter("status", "1"));
        try {
            updateNotice.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateNotice);
        return ResponseUtils.createSuccessResponseBody("删除成功");
    }

    @Override
    public JSONObject recoverNotice(Map<String, Object> params) throws BaseException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("公告id不能为空");
        //恢复公告
        UpdateParams updateNotice = UpdateParams.createUpdateParams("t_noticesInfo");
        updateNotice.addWhereParameter(Parameter.createParameter("id", id));
        updateNotice.addParams(Parameter.createParameter("status", "0"));
        try {
            updateNotice.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.updateDataByParams(updateNotice);
        return ResponseUtils.createSuccessResponseBody("恢复成功");
    }

    @Override
    public JSONObject getNotice(Map<String, Object> params) throws BaseException, SQLException, IOException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        //参数校验
        String id = (String) params.get("id");
        if (BaseChecks.hasEmptyStr(id)) throw new BaseException("公告id不能为空");
        //查询
        QueryParams queryNotice = QueryParams.createQueryParams("vb_queryNotices_lrs");
        queryNotice.addQueryParams(Parameter.createParameter("id", id));
        List<Map<String, Object>> mapList = baseDaoComponent.selectDataByParams(queryNotice);
        //clob类型 转 String
        StringUtils.clobToStrForList(mapList, "msg");
        return ResponseUtils.createSuccessResponseBody(mapList.get(0));
    }


    @Override
    public JSONObject addNoticeForDistrict(Map<String, Object> params) throws BaseException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        CollectionUtils.removeMapValueByKeys(params, "virpath");
        //校验参数
        validateParams(params);
        //添加参数
        params.put("id", UUID.randomUUID().toString());
        params.put("status", "0");
        params.put("mod", "1");
        params.put("lasttime", Format.getDate());
        params.put("userid", loginInfo.getId());
        params.put("districtcode", loginInfo.getDistrictcode());
        //添加公告
        InsertParams inserNotice = InsertParams.createInsertParams("t_noticesinfo", params);
        try {
            //日志
            inserNotice.setDbLog(loginInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        baseDaoComponent.insertDataByParams(inserNotice);
        return ResponseUtils.createSuccessResponseBody("添加成功");
    }

    //添加公告校验参数
    private void validateParams(Map<String, Object> params) throws BaseException {
        String title = (String) params.get("title");
        if (BaseChecks.hasEmptyStr(title)) throw new BaseException("公告标题不能为空");

        String msg = (String) params.get("msg");
        if (BaseChecks.hasEmptyStr(msg)) throw new BaseException("公告内容不能为空");

        Integer sortnum = (Integer) params.get("sortnum");
        if (sortnum == null) throw new BaseException("公告优先级不能为空");
    }

    @Override
    public JSONObject queryHomeNotice(Map<String, Object> params) throws IOException, SQLException {
        LoginInfo loginInfo = (LoginInfo) params.remove("userinfo");
        String roleType = loginInfo.getRoletype();
        String districtcode = (String) params.get("districtcode");
        List<Map<String, Object>> rs = null;
        if ("2".equals(roleType) || "1".equals(roleType)) {
            rs = doDistrictManagerQuery(districtcode);
        } else {
            rs = doOtherManagerQuery(districtcode);
        }
        StringUtils.clobToStrForList(rs, "msg");
        int size = rs.size();
        for (Map<String, Object> map : rs) {
            map.put("total", size);
        }
        return ResponseUtils.createSuccessResponseBodyForJiem("成功", rs);
    }

    //区县管理员或市级管理员查询首页公告
    private List<Map<String, Object>> doDistrictManagerQuery(String districtcode) throws SQLException {
        QueryParams queryParams = QueryParams.createQueryParams("vb_queryHomeNotices_lrs");
        queryParams.addQueryParams(Parameter.createParameter("districtcode", districtcode));        //自己区县的公告
        Parameter parameter = Parameter.createParameter("mod", EOperators.包含, "0", "1");          //市级发布的类型为市级和区级的公告
        parameter.setAnd(false);
        queryParams.addQueryParams(parameter);
        queryParams.addOrderColumns("lasttime desc");
        return baseDaoComponent.selectDataByParams(queryParams);
    }

    //其它管理员查询首页公告
    private List<Map<String, Object>> doOtherManagerQuery(String districtcode) throws SQLException {
        QueryParams queryParams = QueryParams.createQueryParams("vb_queryHomeNotices_lrs");
        queryParams.addQueryParams(Parameter.createParameter("districtcode", districtcode));        //自己区县的公告
        Parameter parameter = Parameter.createParameter("mod", EOperators.包含, "0");               //市级发布的类型为市级的公告
        parameter.setAnd(false);
        queryParams.addQueryParams(parameter);
        queryParams.addOrderColumns("lasttime desc");
        return baseDaoComponent.selectDataByParams(queryParams);
    }
}
